//
//  TicTacGrid.m
//  TicTacToe
//
//  Created by Tim on 09-11-22.
//  Copyright 2009 Tim Dimsdale. All rights reserved.
//
// This file is part of TicTacToe.
// 
// TicTacToe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// TicTacToe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with TicTacToe.  If not, see <http://www.gnu.org/licenses/>
//
#import "TicTacGrid.h"


@implementation TicTacGrid

// constructor
-(id) init {
	self = [super init];
	if ( self ) {
	//	gridSpacing = 50;
		
	}
	return self;
}// init




-(void)drawGrid : (NSRect) rect withModel:(TicTacModel* )model  enabled:(bool)isEnabled {

	[self drawVerticalLines: rect enabled:isEnabled];
	[self drawHorizontalLines: rect enabled:isEnabled];
	
	double cellWidth = rect.size.width / 3.0;
	double cellHeight = rect.size.height / 3.0;

	
	[self drawPlayers:model withCellWidth:cellWidth andCellHeight:cellHeight];
	[self drawWinningLines: cellHeight cellWidth: cellWidth model: model];

	return;
}

-(void)drawVerticalLines : (NSRect) rect   enabled:(bool)isEnabled{
	NSBezierPath* path = [NSBezierPath bezierPath];
	
	[path setLineWidth: GRID_LINE_WIDTH ];

	double offset = rect.size.width / 3.0;
	double height = rect.size.height;
	// draw vertical lines
	for( int xpos=1; xpos<3; xpos++ ) {
		double x =  (xpos*offset);
		[path moveToPoint: 	NSMakePoint( x , 0) ];
		[path lineToPoint: NSMakePoint( x, height) ];
	}// draw vertical peices
	
	if ( isEnabled)
		[[NSColor purpleColor] set];
	else
		[[NSColor darkGrayColor] set];
	[path stroke];
	//NSPoint startPoint = { 10,10 };
	//NSPoint endPoint = { 
	return;
} // draw vertical lines


-(void)drawHorizontalLines : (NSRect) rect  enabled:(bool)isEnabled{
	NSBezierPath* path = [NSBezierPath bezierPath];
	
	[path setLineWidth: GRID_LINE_WIDTH ];
	double offset = rect.size.height / 3.0;
	double width = rect.size.width;
	
	// draw vertical lines
	for( int ypos=1; ypos<3; ypos++ ) {
		double y = (ypos * offset );
		[path moveToPoint: 	NSMakePoint( 0, y) ];
		[path lineToPoint: NSMakePoint(width, y) ];
	}// draw horizontal peices
	
	
	if ( isEnabled)
		[[NSColor purpleColor] set];
	else
		[[NSColor darkGrayColor] set];
	
	[path stroke];

	return;
}// draw horizontal lines

- (void) drawPlayers: (TicTacModel *) model  withCellWidth:(double)cellWidth andCellHeight:(double)cellHeight
{
	for ( int x=0; x<3; x++ ) {
		double xMin = (x*cellWidth) + CELL_SPACING;
		for( int y=0; y<3; y++ ) {
			double yMin = (y*cellHeight)+CELL_SPACING;
			double yMax = ((y+1)*cellHeight)-CELL_SPACING;
			int modeAtCell = [model getPlayerAtX:x andY:y];
			switch ( modeAtCell ) {
				case TICTAC_X:
		
					[self drawPlayerXAtX: xMin yMin:yMin xMax:(((x+1)*cellWidth) -CELL_SPACING) yMax:yMax ];
					 break;
				case TICTAC_O:
					[self drawPlayerOAtX: xMin yMin:yMin width:(cellWidth- 2*CELL_SPACING) height:(cellHeight- 2*CELL_SPACING) ];
					break;
			}// draw differently depending on what it is.
		}// for each vertical
	}// for each horizontal
}// drawCellContents

-(void) drawPlayerXAtX: (double)xMin yMin:(double)yMin xMax:(double)xMax yMax:(double)yMax 
{
	NSBezierPath* path = [NSBezierPath bezierPath];
	[path setLineWidth: GRID_PEOPLE_WIDTH];
	
	[path moveToPoint: NSMakePoint(xMin,yMin)];
	[path lineToPoint: NSMakePoint(xMax,yMax)];
	
	// draw the / part of an x
	[path moveToPoint: NSMakePoint(xMin,yMax)];
	[path lineToPoint: NSMakePoint(xMax,yMin)];
	
	[[NSColor redColor] set];
	[path stroke];	
}

-(void) drawPlayerOAtX: (double) xMin yMin:(double)yMin width:(double)width height:(double)height
{
	NSBezierPath* path = [NSBezierPath bezierPath];
	[path setLineWidth: GRID_PEOPLE_WIDTH];
	
	[path moveToPoint: NSMakePoint(xMin,yMin)];
	[path appendBezierPathWithOvalInRect: NSMakeRect(xMin, yMin,width, height)];
	
	[[NSColor blueColor] set];
	[path stroke];
}

- (void) drawWinningLines: (double) cellHeight cellWidth: (double) cellWidth model: (TicTacModel *) model  {
	// check horizontal lines first
	[[NSColor magentaColor] set];
	NSBezierPath* winningLine = [NSBezierPath bezierPath];
	[winningLine setLineWidth: GRID_WINLINE_WIDTH];
	
	// draw any winning horizontal lines
	for(int x=0; x<3; x++ ) 
	{
		if ( [model getPlayerAtX:x andY:0] != TICTAC_NOBODY && [model getPlayerAtX:x andY:0] == [model getPlayerAtX:x andY:1] && [model getPlayerAtX:x andY:1] ==[model getPlayerAtX:x andY:2] ) {
			int xMin = (x*cellWidth) + ( cellWidth / 2);
			[winningLine moveToPoint: NSMakePoint(xMin,0)];
			[winningLine lineToPoint: NSMakePoint(xMin,cellHeight*3)];
		}
	}
	
	// draw any winning vertical lines
	for(int y=0; y<3; y++ ) 
	{
		if ([model getPlayerAtX:0 andY:y] != TICTAC_NOBODY && [model getPlayerAtX:0 andY:y] == [model getPlayerAtX:1 andY:y] && [model getPlayerAtX:1 andY:y] == [model getPlayerAtX:2 andY:y] ) {
			int yMin = (y*cellHeight) + ( cellHeight / 2 );
			[winningLine moveToPoint: NSMakePoint(0,yMin)];
			[winningLine lineToPoint: NSMakePoint(cellWidth*3, yMin)];
		}
	}			 
	
	// and then the diagonals
	if ( [model getPlayerAtX:0 andY:0] != TICTAC_NOBODY && [model getPlayerAtX:0 andY:0]== [model getPlayerAtX:1 andY:1] &&  [model getPlayerAtX:2 andY:2] == [model getPlayerAtX:1 andY:1] ) 
	{
		[winningLine moveToPoint: NSMakePoint(0,0)];
		[winningLine lineToPoint: NSMakePoint(cellWidth*3, cellHeight*3)];
	}
	//if ( playingTable[2][0] == playingTable[1][1] &&  playingTable[0][2] == playingTable[1][1] ) 
	if ([model getPlayerAtX:2 andY:0] != TICTAC_NOBODY && [model getPlayerAtX:2 andY:0]== [model getPlayerAtX:1 andY:1] &&  [model getPlayerAtX:0 andY:2] == [model getPlayerAtX:1 andY:1] ) 
	{
		[winningLine moveToPoint: NSMakePoint(0,cellHeight*3)];
		[winningLine lineToPoint: NSMakePoint(cellWidth*3, 0)];
	}
	
	
	[winningLine stroke];
	
}// drawWinningLines
@end
